package cz.proks.easyhelpdesk.gwt.ui;

import java.util.Collection;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.easyhelpdesk.api.model.Category;
import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.api.model.User;
import cz.proks.easyhelpdesk.gwt.HelpdeskMessages;
import cz.proks.easyhelpdesk.gwt.event.CategoryDeleteEvent;
import cz.proks.easyhelpdesk.gwt.event.CategorySaveEvent;
import cz.proks.easyhelpdesk.gwt.event.MemberAddToHelpdeskEvent;
import cz.proks.easyhelpdesk.gwt.event.MemberDeleteFromHelpdeskEvent;
import cz.proks.easyhelpdesk.gwt.event.RoleChangeEvent;
import cz.proks.easyhelpdesk.gwt.ui.composite.RestrictedHelpdeskComposite;
import cz.proks.easyhelpdesk.gwt.ui.layout.HelpdeskMainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.layout.IsMainLayoutPanel.ForHelpdesk;
import cz.proks.easyhelpdesk.gwt.ui.widget.CategoriesSettingsWidget;
import cz.proks.easyhelpdesk.gwt.ui.widget.H3Widget;
import cz.proks.easyhelpdesk.gwt.ui.widget.MembersSettingsWidget;

public class SettingsViewImpl extends RestrictedHelpdeskComposite implements SettingsView, MemberAddToHelpdeskEvent.Handler, MemberDeleteFromHelpdeskEvent.Handler, CategorySaveEvent.Handler, CategoryDeleteEvent.Handler, RoleChangeEvent.Handler {

    private static SettingsViewImplUiBinder uiBinder = GWT.create(SettingsViewImplUiBinder.class);

    interface SettingsViewImplUiBinder extends UiBinder<Widget, SettingsViewImpl> {
    }
    
    @UiField HelpdeskMessages i18n;
    @UiField HelpdeskMainLayoutPanel mainLayoutPanel;
    @UiField H3Widget header;
    @UiField TabLayoutPanel tabPanel;
    @UiField MembersSettingsWidget membersSettingsWidget;
    @UiField CategoriesSettingsWidget categoriesSettingsWidget;
            
    private Helpdesk currentHelpdesk;    
    private Presenter presenter;
    
    public SettingsViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }
    
    @Override
    protected void initWidget(Widget widget) {     
        super.initWidget(widget);               
                
        membersSettingsWidget.addMemberAddToHelpdeskHandler(this);
        membersSettingsWidget.addMemberDeleteFromHelpdeskHandler(this);
        membersSettingsWidget.addRoleChangeHandler(this);        
        
        categoriesSettingsWidget.addCategorySaveHandler(this);
        categoriesSettingsWidget.addCategoryDeleteHandler(this);               
    }       
    
    @Override
    protected ForHelpdesk getRootPanel() {
        return mainLayoutPanel;
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;        
    }    
    
   
    @Override
    public void setCurrentHelpdesk(Helpdesk helpdesk) {
        this.currentHelpdesk = helpdesk;
        
        this.header.setText(i18n.settingsForHelpdesk() + " " + helpdesk.getName());
        this.membersSettingsWidget.setHelpdesk(currentHelpdesk);
        this.categoriesSettingsWidget.setHelpdesk(currentHelpdesk);        
    }        
    
    @Override
    public void setHelpdeskUsers(Map<User, Boolean> users) {
        membersSettingsWidget.setMembers(users);        
    }
    
    @Override
    public void setMembersRoles(Map<User, HelpdeskRole> membersRoles) {
        membersSettingsWidget.setMembersRoles(membersRoles);
    }
    
    @Override
    public void setCategories(Collection<Category> categories) {
        categoriesSettingsWidget.getEntities().clear();
        categoriesSettingsWidget.getEntities().addAll(categories);
    }  
    
    @Override
    public void setHelpdeskRoles(Collection<HelpdeskRole> helpdeskRoles) {
        membersSettingsWidget.setHelpdeskRoles(helpdeskRoles);
    }
        
    public void onHelpdeskChange(Helpdesk helpdesk) {
        if (presenter != null)
            presenter.changeSettingsHeldpesk(helpdesk);
    }

    @Override
    public void onCategorySave(CategorySaveEvent event) {
        if (presenter != null)            
            presenter.saveCategory(event.getCategory());        
    }
    
    @Override
    public void onCategoryDelete(CategoryDeleteEvent event) {
        if (presenter != null)            
            presenter.deleteCategory(event.getCategory());
    }

    @Override
    public void onRoleChange(RoleChangeEvent event) {
        if (presenter != null)
            presenter.updateMemberRole(event.getMember(), event.getRole());
    }

    @Override
    public void onMemberDeleteFromHelpdesk(MemberDeleteFromHelpdeskEvent event) {
        if (presenter != null)
            presenter.deleteHelpdeskMember(event.getHelpdesk(), event.getMember());
    }

    @Override
    public void onMemberAddToHelpdesk(MemberAddToHelpdeskEvent event) {
        if (presenter != null)
            presenter.saveHelpdeskMember(event.getHelpdesk(), event.getMember(), event.getRole());
    }        
    
    @Override
    public void setAddMemberId(String userId) {
        membersSettingsWidget.setNewUserId(userId != null && !userId.isEmpty() ? Long.valueOf(userId) : null);
    }

    @Override
    public void setAddCategoryLabel(String label) {
        categoriesSettingsWidget.setNewCategoryLabel(label);
    }

    @Override
    public void onErase() {     
        super.onErase();
        
        membersSettingsWidget.onErase();
        categoriesSettingsWidget.onErase();
    }
}